<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','groupid','image','remark','price','ishot','paixu','createtime');
	$list=pdo_getslice('jy_mapfuwu_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_meiye/sever//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
			array('op'=>'edit','title'=>'添加项目'),
		),
		'list'=>array('data'=>'list','width'=>'15','tip'=>'这里设置平台项目，网点可以选择这里的项目!','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('type'=>'select','name'=>'groupid','title'=>'项目分类','options'=>'groups'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['pattern']=array('0'=>'到店维修','1'=>'上门维修');
	$return['groups']=pdo_getall('jy_mapfuwu_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_mapfuwu_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['hdps']=$item['hdps']?json_decode($item['hdps'],true):array();
	$item['fileds']=$item['fileds']?json_decode($item['fileds'],true):array();
	$return['item']=$item;
	$return['item']['serve_time']=$return['item']['serve_time'] ? json_decode($return['item']['serve_time'],true):array();
	$return['item']['package']=json_decode($return['item']['package']);
	$return['item']['time_box']=$return['item']['time_box']?json_decode($return['item']['time_box'],true):array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'项目名称','name'=>'title','help'=>'','ismust'=>true),
								array('type'=>'image','title'=>'项目图片','name'=>'image','help'=>'','ismust'=>true),
								array('type'=>'images','title'=>'项目幻灯片','name'=>'hdps','help'=>''),
								array('type'=>'select','name'=>'groupid','title'=>'项目分类','options'=>'groups'),
								//array('type'=>'checkbox','title'=>'维修方式','name'=>'serve_types','help'=>'','options'=>'serve_types'),
								array('type'=>'string','title'=>'项目价格','name'=>'price','help'=>'','ismust'=>true),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>'设置热门'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'text','title'=>'项目说明','name'=>'remark','help'=>''),
								array('type'=>'richtext','title'=>'项目详情','name'=>'content','help'=>''),
							)
						),
			)
	);
	//$return['serve_types']=$this->serve_types;
	$return['ismust']=array('0'=>'否','1'=>'是');
	$return['groups']=pdo_getall('jy_mapfuwu_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写项目标题','error');
	}
	if(!$postdata['groupid']){
		exi('请填写项目分类','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'serve_types'=>trim($postdata['serve_types']),
		'image'=>trim($postdata['image']),
		'price'=>intval($postdata['price']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
		'ishot'=>intval($postdata['ishot']),
		'paixu'=>intval($postdata['paixu']),
		'groupid'=>intval($postdata['groupid']),
		'hdps'=>ijson_encode($postdata['hdps']),
		'content'=>trim($postdata['content']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_mapfuwu_serve',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_mapfuwu_serve',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_mapfuwu_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_mapfuwu_serve',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_mapfuwu_serve',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='choose'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_mapfuwu_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_meiye/sever//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}